<template>
  <div class="page-container">
    <!-- 搜索栏 -->
    <div class="search-bar">
      <el-form :model="searchForm" inline>
        <el-form-item label="商家名称">
          <el-input
            v-model="searchForm.merchantName"
            placeholder="搜索商家名称"
            clearable
            style="width: 200px"
          />
        </el-form-item>
        <el-form-item label="分类">
          <el-select
            v-model="searchForm.categoryId"
            placeholder="选择分类"
            clearable
            style="width: 150px"
          >
            <el-option
              v-for="category in categories"
              :key="category.id"
              :label="category.name"
              :value="category.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleSearch">
            <el-icon><Search /></el-icon>
            搜索
          </el-button>
          <el-button @click="handleReset">重置</el-button>
        </el-form-item>
      </el-form>
    </div>

    <!-- 商家列表 -->
    <div class="merchant-grid">
      <el-card
        v-for="merchant in merchants"
        :key="merchant.id"
        class="merchant-card"
        shadow="hover"
        @click="viewMerchant(merchant)"
      >
        <div class="merchant-header">
          <div class="merchant-avatar">
            <el-avatar :size="60" :src="merchant.logo">
              {{ merchant.name ? merchant.name.charAt(0) : '?' }}
            </el-avatar>
          </div>
          <div class="merchant-info">
            <h3 class="merchant-name">{{ merchant.shopName || merchant.name }}</h3>
            <p class="merchant-desc">{{ merchant.description }}</p>
            <div class="merchant-meta">
              <span class="rating">
                <el-icon><Star /></el-icon>
                {{ merchant.rating || '暂无评分' }}
              </span>
              <span class="status" :class="getStatusClass(merchant.status)">
                {{ getStatusText(merchant.status) }}
              </span>
            </div>
          </div>
        </div>
        
        <div class="merchant-footer">
          <div class="merchant-stats">
            <span class="stat-item">
              <el-icon><Clock /></el-icon>
              营业时间: {{ merchant.businessHours || '全天营业' }}
            </span>
            <span class="stat-item">
              <el-icon><Location /></el-icon>
              {{ merchant.address || '暂无地址' }}
            </span>
          </div>
          <el-button type="primary" size="small" @click.stop="viewMerchant(merchant)">
            查看菜单
          </el-button>
        </div>
      </el-card>
    </div>

    <!-- 分页 -->
    <div class="pagination-wrapper">
      <el-pagination
        v-model:current-page="currentPage"
        v-model:page-size="pageSize"
        :total="total"
        :page-sizes="[12, 24, 36, 48]"
        layout="total, sizes, prev, pager, next, jumper"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </div>

    <!-- 加载状态 -->
    <div v-if="loading" class="loading-container">
      <el-skeleton :rows="3" animated />
    </div>

    <!-- 空状态 -->
    <el-empty
      v-if="!loading && merchants.length === 0"
      description="暂无商家信息"
    />
  </div>
</template>

<script setup>
import { ref, reactive, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import { Search, Star, Clock, Location } from '@element-plus/icons-vue'
import { getMerchants, getCategories } from '../../api/merchant'

const router = useRouter()

const loading = ref(false)
const merchants = ref([])
const categories = ref([])
const currentPage = ref(1)
const pageSize = ref(12)
const total = ref(0)

const searchForm = reactive({
  merchantName: '',
  categoryId: ''
})

const loadData = async () => {
  loading.value = true
  try {
    const params = {
      page: currentPage.value - 1,
      size: pageSize.value,
      name: searchForm.merchantName || undefined,
      categoryId: searchForm.categoryId || undefined
    }
    
    const response = await getMerchants(params)
    merchants.value = response.data.content
    total.value = response.data.totalElements
  } catch (error) {
    console.error('加载商家列表失败:', error)
    ElMessage.error('加载商家列表失败')
  } finally {
    loading.value = false
  }
}

const loadCategories = async () => {
  try {
    const response = await getCategories()
    categories.value = response.data
  } catch (error) {
    console.error('加载分类失败:', error)
  }
}

const handleSearch = () => {
  currentPage.value = 1
  loadData()
}

const handleReset = () => {
  searchForm.merchantName = ''
  searchForm.categoryId = ''
  currentPage.value = 1
  loadData()
}

const handleSizeChange = (val) => {
  pageSize.value = val
  currentPage.value = 1
  loadData()
}

const handleCurrentChange = (val) => {
  currentPage.value = val
  loadData()
}

const viewMerchant = (merchant) => {
  router.push(`/student/merchant/${merchant.id}`)
}

const getStatusClass = (status) => {
  const statusMap = {
    'OPEN': 'status-open',
    'CLOSED': 'status-closed',
    'BUSY': 'status-busy'
  }
  return statusMap[status] || 'status-unknown'
}

const getStatusText = (status) => {
  const statusMap = {
    'OPEN': '营业中',
    'CLOSED': '已打烊',
    'BUSY': '忙碌中'
  }
  return statusMap[status] || '未知状态'
}

onMounted(() => {
  loadData()
  loadCategories()
})
</script>

<style scoped>
.page-container {
  padding: 0;
  background: #f0f2f5;
  min-height: calc(100vh - 60px);
  width: 100%;
  margin: 0;
  box-sizing: border-box;
}

.search-bar {
  background: #fff;
  padding: 24px;
  border-radius: 0;
  margin-bottom: 0;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
}

.search-bar .el-form {
  display: flex;
  flex-wrap: wrap;
  gap: 16px;
  align-items: flex-end;
}

.search-bar .el-form-item {
  margin-bottom: 0;
}

.merchant-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(400px, 1fr));
  gap: 24px;
  margin: 0;
  padding: 24px;
  margin-bottom: 32px;
}

.merchant-card {
  cursor: pointer;
  transition: all 0.3s ease;
  border: 1px solid #e4e7ed;
  border-radius: 12px;
  overflow: hidden;
}

.merchant-card:hover {
  transform: translateY(-4px);
  box-shadow: 0 8px 25px rgba(0, 0, 0, 0.15);
  border-color: #409eff;
}

.merchant-header {
  display: flex;
  align-items: flex-start;
  gap: 16px;
  padding: 20px;
  border-bottom: 1px solid #f0f2f5;
}

.merchant-avatar {
  flex-shrink: 0;
}

.merchant-info {
  flex: 1;
  min-width: 0;
}

.merchant-name {
  font-size: 18px;
  font-weight: 600;
  color: #303133;
  margin: 0 0 8px 0;
  line-height: 1.4;
}

.merchant-desc {
  font-size: 14px;
  color: #606266;
  margin: 0 0 12px 0;
  line-height: 1.5;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
}

.merchant-meta {
  display: flex;
  align-items: center;
  gap: 16px;
}

.rating {
  display: flex;
  align-items: center;
  gap: 4px;
  font-size: 14px;
  color: #f39c12;
  font-weight: 500;
}

.rating .el-icon {
  color: #f39c12;
}

.status {
  padding: 4px 8px;
  border-radius: 4px;
  font-size: 12px;
  font-weight: 500;
}

.status-open {
  background: #f0f9ff;
  color: #67c23a;
  border: 1px solid #b3e19d;
}

.status-closed {
  background: #fef0f0;
  color: #f56c6c;
  border: 1px solid #fbc4c4;
}

.status-busy {
  background: #fdf6ec;
  color: #e6a23c;
  border: 1px solid #f5dab1;
}

.status-unknown {
  background: #f4f4f5;
  color: #909399;
  border: 1px solid #d3d4d6;
}

.merchant-footer {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 16px 20px;
  background: #fafafa;
}

.merchant-stats {
  display: flex;
  flex-direction: column;
  gap: 8px;
  flex: 1;
}

.stat-item {
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: 12px;
  color: #909399;
}

.stat-item .el-icon {
  font-size: 14px;
}

.pagination-wrapper {
  display: flex;
  justify-content: center;
  margin: 0;
  padding: 32px 24px;
}

.loading-container {
  padding: 40px;
}

/* 响应式设计 */
@media (max-width: 1200px) {
  .merchant-grid {
    grid-template-columns: repeat(auto-fill, minmax(350px, 1fr));
    gap: 20px;
  }
  
  .merchant-header {
    padding: 16px;
  }
  
  .merchant-footer {
    padding: 12px 16px;
  }
}

@media (max-width: 768px) {
  .merchant-grid {
    grid-template-columns: 1fr;
    gap: 16px;
  }
  
  .search-bar {
    padding: 16px;
  }
  
  .search-bar .el-form {
    flex-direction: column;
    align-items: stretch;
  }
  
  .search-bar .el-form-item {
    margin-bottom: 16px;
  }
  
  .merchant-header {
    flex-direction: column;
    text-align: center;
  }
  
  .merchant-footer {
    flex-direction: column;
    gap: 12px;
    align-items: stretch;
  }
}

@media (min-width: 1600px) {
  .merchant-grid {
    grid-template-columns: repeat(auto-fill, minmax(450px, 1fr));
    gap: 32px;
  }
  
  .merchant-header {
    padding: 24px;
  }
  
  .merchant-footer {
    padding: 20px 24px;
  }
}

/* 动画效果 */
.merchant-card {
  animation: fadeInUp 0.6s ease-out;
}

@keyframes fadeInUp {
  from {
    opacity: 0;
    transform: translateY(20px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

/* 卡片内容优化 */
.merchant-card .el-card__body {
  padding: 0;
}

.merchant-card .el-card__header {
  display: none;
}
</style>
